﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Commons;

public partial class OperaEntry_TypeEdit : System.Web.UI.Page
{
    DataSet ds;

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                DbHelper helper = DbHelper.getHelper();
                ds = helper.ExcuteDataSetQuery("Select * from types");
            }
            catch (Exception ex)
            {
                this.MessageLabel.Text = ex.Message;
            }

            AddTree(0, (TreeNode)null);
        }
    }

    public void AddTree(int ParentID, TreeNode pNode)
    {
        TreeNode tn1 = new TreeNode();
        DataView dvTree = new DataView(ds.Tables[0]);
        //过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "fatherid = " + ParentID;

        foreach (DataRow Row in dvTree.ToTable().Rows)
        {
            if (pNode == null)
            {
                tn1.Text = Row["typename"].ToString();
                TypeTree.Nodes.Add(tn1);
                tn1.Expanded = true;
                tn1.NavigateUrl = "javascript:selectNode('" + Row["typeid"].ToString() + "','" + Row["typename"].ToString() + "');";
                AddTree(Int32.Parse(Row["typeid"].ToString()), tn1);    //再次递归
            }
            else
            {   //添加当前节点的子节点
                TreeNode tn2 = new TreeNode();
                tn2.Text = Row["typename"].ToString();
                pNode.ChildNodes.Add(tn2);
                tn1.Expanded = true;

                tn2.NavigateUrl = "javascript:selectNode('" + Row["typeid"].ToString() + "','" + Row["typename"].ToString() + "');";
                AddTree(Int32.Parse(Row["typeid"].ToString()), tn2);    //再次递归
            }
        }
    }
}
